package com.example.annoaop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 增强类
 * @Author: wyp
 * @Date: 2021/9/2 10:06
 */
@Component
@Aspect
public class HelloStrengthen {

    /**
     * 抽取相同的切入点
     */
    @Pointcut(value = "execution(* com.example.annoaop.Hello.say(..))")
    public void pointcut(){}

    /**
     * 前置通知
     */
    @Before(value = "pointcut()")
    public void before(){
        System.out.println("增强的代码before");
    }

    /**
     * 后置通知
     */
    @AfterReturning(value = "execution(* com.example.annoaop.Hello.say(..))")
    public void afterReturning(){
        System.out.println("增强的代码afterReturning");
    }

    /**
     * 环绕通知
     */
    @Around(value = "execution(* com.example.annoaop.Hello.say(..))")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("增强的代码around前");
        Object proceed = proceedingJoinPoint.proceed();
        System.out.println("增强的代码around后");
        return proceed;
    }

    /**
     * 最终通知
     */
    @After(value = "pointcut()")
    public void after(){
        System.out.println("增强的代码after");
    }

    /**
     * 异常通知
     */
    @AfterThrowing(value = "execution(* com.example.annoaop.Hello.say(..))")
    public void afterThrowing(){
        System.out.println("增强的代码afterThrowing");
    }
}
